home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / wb-enhancement / swazblanker / programmer / dev / melt.c < prev    next >
C/C++ Source or Header  |  1996-02-26  |  12KB  |  550 lines

  1.  
  2. /*
  3. ** Example blanker module source for Melt Blanker.
  4. **
  5. **    (C) 1993,94 David Swsbrook,
  6. **    All Rights Reserved.
  7. **
  8. */
  9.  
  10. #include "usr:dev/source/Sprintf.c"
  11.  
  12. /*
  13. ** Firstly we must include the version strings
  14. ** and stuff.
  15. */
  16.     #include    "Blanker.ver"
  17.  
  18.     extern UBYTE VersionStr[] = VERSIONSTR;
  19.     extern UBYTE TimeStr[]    = TIMESTR;
  20.     extern UBYTE UserStr[]    = USERSTR;
  21.     extern UBYTE ReleseStr[]  = RELEASESTR;
  22.     extern UBYTE CommentStr[] = COMMENTSTR;
  23.     extern UBYTE NameStr[]    = PROGNAME_NOVER;
  24.     extern UBYTE NameVerStr[] = PROGNAME_VER;
  25.     extern UBYTE Name[]       = "Melt";
  26.  
  27. /*
  28. ** SWAZBLANKER INIT STRUCT
  29. */
  30.     struct SBInit *SBInit;
  31.  
  32. /*
  33. ** This string allows BlankerServer to identify
  34. ** us as a blanker module. If this is not present
  35. ** then BlankerServer will not load us.
  36. */
  37. extern UBYTE ID[] = "\0$BLANKER:Ver 2:";
  38.  
  39. /*
  40. ** *** LOCALE SUPPORT
  41. **
  42. ** It is desired that your blanker supports locale. If you are using matrix.library
  43. ** then this is easily done. Using the progran "cd2c" you can create a C file which
  44. ** includes #defines for the locale ID's and an array of ID Offsets and a string base.
  45. ** Matrix.library has a function MX_GetCatalogStr() which can parse these arrays and
  46. ** return you the locale string.
  47. */
  48.     #include  "MeltCat.c"
  49.  
  50.     #define LOCALE(o) MX_GetCatalogStr(SBInit->Catalog, (ULONG *) &LocArray, o)
  51.  
  52.  
  53.     struct BPrefs {
  54.         UBYTE    Pad;
  55.     };
  56.  
  57.     struct BPrefs BPrefs;
  58.  
  59.     struct MatrixBase *MatrixBase;
  60.     struct SwazConfigBase *SwazConfigBase;
  61.     struct SwazBlankerBase *SwazBlankerBase;
  62.  
  63.  
  64. /******************************************************************
  65. **
  66. **    MISC SUPPORT FUNCTIONS
  67. **
  68. ******************************************************************/
  69.  
  70.     BOOL active;    /* Global, set by Blanker() routine */
  71.  
  72. BOOL IsActive( void )
  73. {
  74.     if(active) active = !(SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C);
  75.     return( active );
  76. }
  77.  
  78. /**
  79. *** CloneTagItemsTags()
  80. **/
  81. struct TagItem *CloneTagItemsTags( Tag tag1, ... )
  82. {
  83.     return( CloneTagItems( (struct TagItem *) &tag1 ) );
  84. }
  85.  
  86. /**
  87. *** LongestString()
  88. **/
  89. ULONG LongestString( ULONG ID, ... )
  90. {
  91.     ULONG *z;
  92.     ULONG r = 0, c;
  93.  
  94.     for( z = &ID; *z; z++ )
  95.     {    c = strlen( LOCALE( *z ) );
  96.         if( c > r ) r = c;
  97.     }
  98.     return( r );
  99. }
  100.  
  101.  
  102.  
  103. /******************************************************************
  104. **
  105. **    BLANKER
  106. **
  107. ******************************************************************/
  108.  
  109.     struct    Screen *sc = NULL;
  110.     struct    Window *wi = NULL;
  111.     struct    DrawInfo *di = NULL;
  112.  
  113. void DisplayCleanUp( void )
  114. {
  115.     if(di) FreeScreenDrawInfo( sc, di );
  116.     if( wi ) CloseWindow( wi ); wi = NULL;
  117.     if( sc ) CloseScreen( sc ); sc = NULL;
  118. }
  119.  
  120. BOOL DisplaySetUp( void )
  121. {
  122.     if(!(SB_CloneFrontScreenTagList( &sc, &wi, NULL )) )    return(FALSE);
  123.     if(!(di = GetScreenDrawInfo( sc )))            return(FALSE);
  124.  
  125.     return(TRUE);
  126. }
  127.  
  128.  
  129. void CleanUp(void)
  130. {
  131.     DisplayCleanUp();
  132. }
  133.  
  134. BOOL SetUp(void)
  135. {
  136.     if(!DisplaySetUp())    return(FALSE);
  137.     return(TRUE);
  138. }
  139.  
  140.  
  141.  
  142.  
  143.  
  144. #define    RPORT    wi->RPort
  145.  
  146. void    melt(int    valy)
  147. {
  148.     int    x, y,            /* start positions         */
  149.         dx, dy,            /* offsets                 */
  150.         u, v;            /* size                    */
  151.     int    TempA[32];        /* temp buffer             */
  152.     char    mask;            /* bit-plane mask for blitter    */
  153.  
  154.     for(mask=1; mask<+(1<<(RPORT->BitMap->Depth))-1; mask*=2)
  155.     {
  156.     u = MX_Random(wi->Width - 3) +1;
  157.     v = MX_Random(wi->Height - 3) +1;
  158.     x = MX_Random(wi->Width - 2 - u);    // removed +1
  159.     y = MX_Random(wi->Height - 3 - v);     // removed +1
  160.     dx = MX_Random(3) - 1;
  161.     dy = MX_Random(3) - valy;  /* also try dy = MX_Random(3) - 1;  */
  162.  
  163.     BltBitMap(RPORT->BitMap, x, y,
  164.         RPORT->BitMap, x+dx, y+dy,
  165.         u, v, 0xC0,
  166.         mask, (APTR) &TempA);
  167.     }
  168. }
  169.  
  170. void Blanker( void )
  171. {
  172.     active = TRUE;
  173.  
  174.     SetAPen(RPORT,1);
  175.     Move(RPORT,0,0);
  176.     Draw(RPORT,wi->Width-1,0);
  177.     Draw(RPORT,wi->Width-1,wi->Height-1);
  178.     Draw(RPORT,0,wi->Height-1);
  179.     Draw(RPORT,0,0);
  180.  
  181.     if( IsActive() )
  182.     {
  183.      SB_SetBlankerScreen(sc,wi);
  184.      SB_BlankerReady();
  185.      while( IsActive() )
  186.      {
  187.       melt(-1);
  188.       WaitTOF();
  189.      }
  190.      SB_ClrBlankerScreen(sc,wi);
  191.     }
  192. }
  193.  
  194.  
  195.     /****************
  196.          Blank()
  197.     ****************/
  198.  
  199. void Blank( void )
  200. {
  201.     if( SetUp() )
  202.         Blanker();
  203.     CleanUp();
  204. }
  205.  
  206. /******************************************************************
  207. **
  208. **    PREFERENCES DATA
  209. **
  210. ******************************************************************/
  211.  
  212.  
  213.     /*
  214.     ** Preferences global data.
  215.     */
  216.  
  217.     struct WindowHandler *PrefsWH;
  218.     struct BlankerPrefsNode *bpn;
  219.  
  220. /******************************************************************
  221. **
  222. **    PREFERENCES HOOKS
  223. **
  224. ******************************************************************/
  225.  
  226.     /*******************
  227.          SetToDef()
  228.     *******************/
  229.  
  230. void __saveds SetToDef( void )
  231. {
  232. }
  233.  
  234.     /******************
  235.          DoAbout()
  236.     ******************/
  237.  
  238. void __saveds DoAbout( void )
  239. {
  240.     STRPTR versionptr = &VersionStr[6];
  241.     MX_DisplayRequestWin( LOCALE(MSG_LOCALE_ABOUT), LOCALE(MSG_GAD_OK), &versionptr, PrefsWH->wh_Window);
  242. }
  243.  
  244.     /******************
  245.          DoHelp()
  246.     ******************/
  247.  
  248. void __saveds DoHelp( void )
  249. {
  250.     SB_HelpTags( &*Name, PrefsWH->wh_Window,
  251.         SBHELP_BlankerPrefs, bpn,
  252.         TAG_DONE);
  253. }
  254.  
  255.  
  256.     /****************
  257.          DoTest()
  258.     ****************/
  259.  
  260. void __saveds DoTest( void )
  261. {
  262.     struct SBMessage *sbm;
  263.  
  264.     SetSignal( 0, SIGBREAKF_CTRL_C );
  265.     if( sbm = SB_BecomeBlankerTaskTagList( NULL ) )
  266.     {
  267.         Blank();
  268.         SB_BecomeBlankerTaskEnd( sbm );
  269.     }
  270. }
  271.  
  272.     /*******************
  273.          DoRawKey()
  274.     *******************/
  275.  
  276. BOOL __saveds __asm DoRawKey( register __a0 struct WindowHandler *wh, register __a1 struct IntuiMessage *im)
  277. {
  278.     #define  RAWKEY_HELP 0x5F
  279.  
  280.     switch(im->Code) {
  281.       case RAWKEY_HELP:
  282.         DoHelp();
  283.         return(TRUE);
  284.       default:
  285.         return(FALSE);
  286.     }
  287. }
  288.     ULONG PrefsIDCMP[] = {
  289.         RAWKEY, (ULONG) DoRawKey,
  290.         NULL,
  291.     };
  292.  
  293. /******************************************************************
  294. **
  295. **    PREFERENCES MAIN
  296. **
  297. ******************************************************************/
  298.  
  299. void Prefs_BuildGadgets( APTR gadbase, WORD *width )
  300. {
  301.     #include <clib/mxg_protos.h>    /* We need these includes */
  302.  
  303.     WORD bw,bg;
  304.     WORD lt;
  305.     WORD s;
  306.  
  307.  
  308.     APTR d = gadbase;
  309.  
  310.     bw = 2+LongestString( MSG_GAD_SAVE, MSG_GAD_USE, MSG_GAD_TEST, MSG_GAD_CANCEL, NULL );
  311.  
  312.     bg = 2;
  313.     *width = 2+2+(bg*3)+(bw*4);
  314.     lt = 4+LongestString( MSG_NO_CONFIG );
  315.  
  316.     s = 0;
  317.     while( lt > *width )
  318.     {
  319.       if( s ) {
  320.         bg++;
  321.         s = 1;
  322.       } else {
  323.         s = 0;
  324.         bw++;
  325.       }
  326.       *width = 2+2+(bg*3)+(bw*4);
  327.     }
  328.     bg += bw;
  329.  
  330.     if( lt < *width )
  331.       lt = *width;
  332.  
  333.  
  334.     d = mxg_HorizLine( d, 3, -2 ); 
  335.     d = mxg_Button( d, ID_VBUT, 2,        3, bw, MSG_GAD_SAVE,   0, 2 );
  336.     d = mxg_Button( d, ID_VBUT, 2+(bg),   3, bw, MSG_GAD_USE,    0, 1 );
  337.     d = mxg_Button( d, ID_BUT,  2+(bg*2), 3, bw, MSG_GAD_TEST,   0, (ULONG) DoTest );
  338.     d = mxg_Button( d, ID_VBUT, 2+(bg*3), 3, bw, MSG_GAD_CANCEL, 0, 0 );
  339.  
  340.  
  341.  
  342.     d = mxg_Text( d, 0, 1, *width, MSG_NO_CONFIG, PLACETEXT_IN, NULL);
  343.     mxg_End( d );
  344.  
  345.     /* Un-comment the following lines to get a requestor displaying the number of bytes that
  346.        your interface layout uses. I will improve the mxg.lib to track and allocate the
  347.        gadget data.... but later...
  348.  
  349.        We add an extra 2 bytes since mxg_End() does not increment pointer!
  350.  
  351.     d = ( (ULONG) d - (ULONG) gadbase ) + 2; 
  352.     MX_DisplayRequestWin("Bytes used for interface layout : %ld\n", 0, &d, 0 );
  353.  
  354.     */
  355.  
  356. }
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.     STRPTR CFT[] = {
  369.         NULL
  370.          };
  371.  
  372.  
  373. void DefaultPrefs( struct BPrefs *p )
  374. {
  375.  
  376. }
  377.  
  378. void LoadPrefs( struct BPrefs *p, STRPTR FileName )
  379. {
  380.  
  381.     APTR cf;
  382.     // struct SC_ConfigVar *cv;
  383.  
  384.     DefaultPrefs( p );
  385.  
  386.     if( cf = SC_ReadConfig( FileName, &*Name ) )
  387.     {
  388.  
  389.       SC_FreeConfig( cf );
  390.     }
  391. }
  392.  
  393. void SavePrefs( struct BPrefs *p, STRPTR FileName )
  394. {
  395.     APTR cf;
  396.     UBYTE V[32];
  397.  
  398.     if( cf = SC_ReadConfig( FileName, &*Name ) )
  399.     {
  400.  
  401.       SC_WriteConfig( cf, FileName, &*Name );
  402.       SC_FreeConfig( cf );
  403.     }
  404. }
  405.  
  406. ULONG __saveds __asm PrefsCallBack( register __a0 struct Hook *Hook, register __a2 APTR Object, register __a1 ULONG *Msg )
  407. {
  408.     ULONG result;
  409.  
  410.     result = ~0;
  411.  
  412.     switch( *Msg ) {
  413.       case MXPFCB_DEFAULT:
  414.         DefaultPrefs( &BPrefs );
  415.         break;
  416.       case MXPFCB_LOAD:
  417.         LoadPrefs( &BPrefs, Object );
  418.         result = 0;
  419.         break;
  420.  
  421.       case MXPFCB_SAVE:
  422.         SavePrefs( &BPrefs, Object );
  423.         result = 0;
  424.         break;
  425.     }
  426.  
  427.     return( result );
  428. }
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435. /****** Prefs() *************************************************
  436. ****************************************************************/
  437. void Prefs(void)
  438. {
  439.     #define GADGET_DATA_SIZE 200        /* Make sure you set this size correctly */
  440.     struct TagItem *WHTags;        /* Window Handler Tags */
  441.     UBYTE fontname[40];            /* Storage for the font name */
  442.     APTR gadget_data;            /* Gadget data base and reference */
  443.     WORD width;
  444.  
  445.  
  446.     struct Hook PrefsCallBackHook;
  447.  
  448.     MX_InitHook( &PrefsCallBackHook, PrefsCallBack, 0 );
  449.     if( bpn = SB_AddPrefsTaskTagList( &*NameStr, NULL ) )
  450.     {
  451.       if( gadget_data = AllocVec( 200, 0 ) )
  452.       {
  453.         Prefs_BuildGadgets( gadget_data, &width );
  454.         SB_GetBaseVarTags(
  455.             SBBV_FontInfo, &*fontname,
  456.             TAG_DONE);
  457.  
  458.         WHTags = CloneTagItemsTags(
  459.             MXWH_CloseWindow,    0,
  460.             MXWH_ESCAddress,    -1,
  461.             MXWH_WindowSize,    (width<<16)|4,
  462.                         MXWH_CatCompArray,    LocArray,               /* Locale array as created by "cd2c", see MX_GetCatalogStr() for other array types supported */
  463.             MXWH_Catalog,    SBInit->Catalog,
  464.             MXWH_WindowPointer,    (ULONG) &bpn->window,
  465.             MXWH_WindowTitle,    (ULONG) &NameVerStr,
  466.             MXWH_ScreenTitle,    (ULONG) &VersionStr[6],
  467.             MXWH_Gadgets,    (ULONG) gadget_data,
  468.             MXWH_WindowHandlerStore, (ULONG) &PrefsWH,
  469.             MXWH_IDCMPFlags,    RAWKEY,
  470.             MXWH_Events,     (ULONG) PrefsIDCMP,
  471.             TAG_DONE);
  472.  
  473.  
  474.         if( WHTags ) {
  475.           MX_NewPrefsWHTags(
  476.               MXPF_UseFile,         SwazBlankerBase->UseFileName,
  477.               MXPF_SaveFile,        SwazBlankerBase->SaveFileName,
  478.               MXPF_CallBack,        &PrefsCallBackHook,
  479.               MXPF_WHTags,          WHTags,                 /* WindowHandler tags (see MX_WindowHandler()) */
  480.               MXPF_Title,           NameStr,                /* Preferences name */
  481.               MXPF_InitialLoad,     TRUE,                   /* Load preferences file */
  482.               MXPF_AboutMenuHook,   DoAbout,                /* Hook function for About Menu item */
  483.               MXPF_HelpMenuHook,    DoHelp,                 /* Hook function for Help Menu item */
  484.               MXPF_Font,            fontname,               /* Name of font to use, "fontname size", eg. "topaz 8" */
  485.               TAG_DONE);
  486.  
  487.           FreeTagItems( WHTags );
  488.         }
  489.         FreeVec( gadget_data );
  490.       }
  491.       SB_RemPrefsTask( bpn );
  492.     }
  493. }
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500. /*
  501. ** Here is a list of the libries we wished to have available.
  502. */
  503.  
  504.  
  505.     #include "usr:dev/source/OpenLibrary.c"
  506.  
  507.     struct LibArray LibData[] = {
  508.         &*MxLibName,           24, (APTR) &MatrixBase,
  509.         "swazblanker.library", 42, (APTR) &SwazBlankerBase,
  510.         "swazconfig.library",   1, (APTR) &SwazConfigBase,
  511.         "intuition.library",   39, (APTR) &IntuitionBase,
  512.         "graphics.library",    39, (APTR) &GfxBase,
  513.         "gadtools.library",    39, (APTR) &GadToolsBase,
  514.         NULL,NULL
  515.     };
  516.  
  517. void main (int argc, char **argv)
  518. {
  519.     if( OpenLibs(&*NameStr,39,&*LibData) )
  520.     {
  521.         if( SBInit = SB_InitTags( SBINIT_Name,&*Name, TAG_DONE ) )
  522.         {
  523.             switch( SBInit->Method )
  524.             {
  525.                 case SBINIT_METHOD_BLANK:
  526.         if( SwazBlankerBase->UseFileName ) LoadPrefs( &BPrefs, SwazBlankerBase->UseFileName );
  527.         else LoadPrefs( &BPrefs, SwazBlankerBase->SaveFileName );
  528.         Blank();
  529.                     break;
  530.                 case SBINIT_METHOD_PREFS:
  531.                     Prefs();
  532.                     break;
  533.                 case SBINIT_METHOD_INFO:
  534.         SB_PrintInfoTags( SBInit,
  535.             SBINFO_Author,      ~0,    // "David Swasbrook (Swaz)",
  536.             SBINFO_EMail,       ~0,    // "swaz@iconz.co.nz",
  537.             SBINFO_ShortDesc,   LOCALE( MSG_DESCRIPTION_SHORT ),
  538.             SBINFO_LongDesc,    LOCALE( MSG_DESCRIPTION_LONG ),
  539.             SBINFO_Version,     VERSION,
  540.             SBINFO_Revision,    REVISION,
  541.             SBINFO_Time,        &TimeStr[6],
  542.             SBINFO_CPULoading,  SBINFOLOAD_MEDIUM,
  543.             TAG_DONE );
  544.             }
  545.             SB_FreeInit( SBInit );
  546.         }
  547.     }
  548.     CloseLibs(&*LibData);
  549. }
  550.